﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using System.Threading;
using Relocation.Com.Tools;
using Relocation.Service;
using Relocation.Base;
using Relocation.Data;
using Relocation.Com;

namespace Relocation
{
	public partial class AreaCompensateWindow : Base.UI.BaseForm
	{

		#region 变量声明
		
		/// <summary>
		/// 当前Villages对象
		/// </summary>
		Village Villages;

		/// <summary>
		/// 合同文件对象
		/// </summary>
		File HTFiles;

		/// <summary>
		/// 规划图
		/// </summary>
		File PicFiles;

		/// <summary>
		/// 当前Area 
		/// </summary>
		Area Area;
		/// <summary>
		/// 当前Ccompensate
		/// </summary>
		Compensate Ccompensate;
		#endregion


		#region 构造函数
		public AreaCompensateWindow():base()
		{
			
		}

		public AreaCompensateWindow(Session session, Village village)
			: base(session)
		{
			InitializeComponent();			
			this.Villages = village;
			if (this.Villages == null)
				return;

			this.Villages.Files.Load();
			this.HTFiles = this.Villages.Files.FirstOrDefault(t => t.file_type == 0);
			this.Session.Project.Files.Load();
			this.PicFiles = this.Session.Project.Files.FirstOrDefault(t => t.file_type == 1);
		}

		#endregion

		/// <summary>
		/// 窗体加载事件
		/// </summary>		
		private void AreaCcompensateWindow_Load(object sender, EventArgs e)
		{
			CheckUser(this.Session);
			if (this.Villages == null)
				return;

			LoadVillagesInfo(this.Villages);

			//加载合同文件
			if (this.HTFiles != null)
			{
				this.LinkLabelFile.Text = this.HTFiles.file_name;
			} else
			{
				this.LinkLabelFile.Text = "请上传文件";
			}

			//加载红线规划图
			if (this.PicFiles != null)
			{
				Tools.ShowImage(PictureBox1, this.PicFiles.path);
			} else
			{
				PictureBox1.Image = (System.Drawing.Image)(Relocation.Com.Resource.nopic);
			}

			ToolTip toolTip = new ToolTip();
			toolTip.SetToolTip(this.LinkLabelFile, "点击打开");

			//加载Area的记录
			this.RefreshArea();
			//加载Ccompensate的记录
			this.RefreshCcompensate();


		}

		/// <summary>
		/// 将villages显示到文本框中
		/// </summary>
		/// <param name="villages"></param>
		private void LoadVillagesInfo(Village villages)
		{
			this.TextBox_Village_Name.Text = villages.name;
			this.TextBox_Village_Phone_numbe.Text = villages.phone_numbe;
			this.TextBox_Village_Principal.Text = villages.principal;
			this.TextBox_Village_Place.Text = villages.place;
			this.TextBox_Village_Remark.Text = villages.remark;
		}


		/// <summary>
		/// 刷新村庄的信息
		/// </summary>
		public override void Refresh()
		{
			base.Refresh();
			this.RefreshArea();
		}

		#region Villages相关操作

		/// <summary>
		/// 上传合同或者打开合同
		/// </summary>		
		private void LinkLabelFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (this.LinkLabelFile.Text == "请上传文件")
			{
				OpenFileDialog OpenFileDialog = new OpenFileDialog();
				OpenFileDialog.Filter = "Word文档 (*.doc,*.docx)|*.doc;*.docx|所有文件(*.*)|*.*";
				OpenFileDialog.FilterIndex = 0;
				OpenFileDialog.RestoreDirectory = false;
				OpenFileDialog.Multiselect = false;
				OpenFileDialog.RestoreDirectory = false;
				DialogResult DialogResult = OpenFileDialog.ShowDialog();
				if (!DialogResult.Equals(DialogResult.OK))
					return;

				string FilePath = OpenFileDialog.FileName;
				string FileName = System.IO.Path.GetFileName(FilePath);
				//string DocFolder = Tools.GetStartupPath() + @"\docs\" + System.DateTime.Now.ToFileTime().ToString() + @"\";
				string NewFilePath = Tools.SaveFileToDocs(FilePath);// DocFolder + FileName;
				//if (!Directory.Exists(DocFolder))
				//	Directory.CreateDirectory(DocFolder);

				this.LinkLabelFile.Text = FileName;

				this.Villages.Files.Load();
				File Files = this.Villages.Files.FirstOrDefault();
				if (Files == null)
				{
					Files = new File();
					Files.Project = this.Session.Project;
					Files.Village = this.Villages;
					Files.file_id = DataModel.GenerateID(Files);
					Files.file_type = 0;//合同为0，规划红线图为1
				}

				Files.file_name = FileName;
				Files.path = NewFilePath;

				this.Session.DataModel.Save(Files);
				this.HTFiles = Files;
				this.LinkLabelFile.Text = FileName;
				MyMessagebox.Show("文件上传成功");
			} else
			{
				try
				{
					if (this.HTFiles == null)
					{
						throw new Exception("Villages中不包含Files,ID：" + Villages.village_id.ToString() + "\tName：" + Villages.name);
					}

					Tools.OpenFile(this.HTFiles.path);
				} catch (Exception ex)
				{
					Log.Error(Tools.GetExceptionMessage(ex));
					MyMessagebox.Error(ex);
				}

			}
		}

		/// <summary>
		/// 删除合同文件
		/// </summary>		
		private void ButtonDeleteFile_Click(object sender, EventArgs e)
		{
			try
			{
				this.Villages.Files.Load();
				if (this.Villages.Files.Count < 1)
					return;
				DialogResult dialogResult = MyMessagebox.Confirm("您确定要删除文件吗？");
				if (!dialogResult.Equals(DialogResult.Yes))
					return;

				while (this.Villages.Files.Count > 0)
				{
					File files = this.Villages.Files.First();
					string path = files.path;
					this.Session.DataModel.Delete(this.Villages.Files.First());
					Tools.DeleteFile_Directory(path);
				}

				this.LinkLabelFile.Text = "请上传文件";
			} catch (Exception ex)
			{
				MyMessagebox.Error(ex);
				Log.Error(Tools.GetExceptionMessage(ex));
			}

		}

		private void ButtonOpenPic_Click(object sender, EventArgs e)
		{
			if (this.PicFiles == null)
			{
				MyMessagebox.Show("没有相关图片");
			} else
			{
				try
				{
					Tools.OpenFile(this.PicFiles.path);
				} catch (Exception)
				{
					string extension = System.IO.Path.GetExtension(this.PicFiles.path);
					MyMessagebox.Show("您的电脑不支持“" + extension + "”类型的文件，请安装相应的应用程序！");
				}
			}
		}



		#endregion

		#region Area相关

		#region Area按钮操作
		/// <summary>
		/// Area保存或修改
		/// </summary>		
		private void ButtonSaveArea_Click(object sender, EventArgs e)
		{
			if (this.ButtonAreaSave.Text == "修改")
			{
				this.Area = GetSelectArea();
				if (this.Area == null)
					return;
				//将内容放入文本框
				SetAreaTextBoxsValues(this.Area);
				//取消文本框的只读
				SetAreaTextBoxsReadonly(false);
				this.ButtonAreaCancel.Text = "取消";
				this.ButtonAreaSave.Text = "保存";

			} else
			{

				if (Area == null)
				{
					Area = new Area();
					Area.area_id = DataModel.GenerateID(Area);
					Area.Village = this.Villages;
					Area.created = System.DateTime.Now;
				}
				if (GetAreaFormInput(ref Area) == null)
				{
					MyMessagebox.Show("输入内容的格式不正确！");
					return;
				}
				this.Session.DataModel.Save(Area);
				SetAreaTextBoxsReadonly(true);
				MessageBox.Show("保存成功！");
				this.Session.DataModel.Refresh(System.Data.Objects.RefreshMode.ClientWins, this.Villages.Areas);
				this.RefreshArea();

				this.ButtonAreaCancel.Text = "新增";
				this.ButtonAreaSave.Text = "修改";


			}
		}

		/// <summary>
		/// Area取消或新增按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ButtonCancelArea_Click(object sender, EventArgs e)
		{
			if (this.ButtonAreaCancel.Text == "新增")
			{
				this.Area = null;

				//将文本框设置为可编辑
				SetAreaTextBoxsReadonly(false);
				//将文本框内容清空
				SetAreaTextBoxsValues(null);
				this.ButtonAreaCancel.Text = "取消";
				this.ButtonAreaSave.Text = "保存";

			} else
			{
				SetAreaTextBoxsReadonly(true);

				Area _area = this.Area;
				if (_area == null)
				{
					_area = GetSelectArea();
				}

				SetAreaTextBoxsValues(_area);
				this.ButtonAreaCancel.Text = "新增";
				this.ButtonAreaSave.Text = "修改";
			}
		}

		/// <summary>
		/// Area删除按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ButtonDeleteArea_Click(object sender, EventArgs e)
		{
			Area area = this.GetSelectArea();
			if (area == null)
				return;
			this.Session.DataModel.Delete(area);
			this.RefreshArea();
		}

		#endregion

		#region Area其他方法
		/// <summary>
		/// 设置Area输入框是否只读
		/// </summary>
		/// <param name="readOnly"></param>
		private void SetAreaTextBoxsReadonly(bool readOnly)
		{
			this.TextBox_Area_Area_code.ReadOnly = readOnly;
			this.TextBox_Area_Area_name.Enabled = !readOnly;
			this.TextBox_Area_Centiare.ReadOnly = readOnly;
			this.TextBox_Area_Group_area.ReadOnly = readOnly;
			this.TextBox_Area_Mu.ReadOnly = readOnly;
			this.TextBox_Area_Remark.ReadOnly = readOnly;
			this.AreaGridView.Enabled = readOnly;
		}

		/// <summary>
		/// 将area数据放到文本框中（如果area为空则清空文本框）
		/// </summary>
		/// <param name="area">Area</param>
		private void SetAreaTextBoxsValues(Area area)
		{
			if (area == null)
			{
				this.TextBox_Area_Area_code.Text = "";
				this.TextBox_Area_Area_name.Text = "";
				this.TextBox_Area_Centiare.Text = "0.00";
				this.TextBox_Area_Group_area.Text = "";
				this.TextBox_Area_Mu.Text = "0.00";
				this.TextBox_Area_Remark.Text = "";
				return;

			}

			this.TextBox_Area_Area_code.Text = area.area_code;
			this.TextBox_Area_Area_name.Text = area.area_name;
			this.TextBox_Area_Centiare.Text = area.centiare.ToString();
			this.TextBox_Area_Group_area.Text = area.group_area;
			this.TextBox_Area_Mu.Text = area.mu.ToString();
			this.TextBox_Area_Remark.Text = area.remark;
		}

		/// <summary>
		/// 更具用户输入的内容初始化Area （如果用户输入的信息不正确则返回null）
		/// </summary>
		/// <param name="area"></param>
		/// <returns></returns>
		private Area GetAreaFormInput(ref Area area)
		{
			if (!CheckArea())
			{
				return null;
			}

			area.area_code = TextBox_Area_Area_code.Text;
			area.area_name = TextBox_Area_Area_name.Text;
			area.centiare = Convert.ToDecimal(this.TextBox_Area_Centiare.Text);
			area.group_area = this.TextBox_Area_Group_area.Text;
			area.mu = Convert.ToDecimal(this.TextBox_Area_Mu.Text);
			area.remark = this.TextBox_Area_Remark.Text;
			area.updated = System.DateTime.Now;
			return area;
		}

		/// <summary>
		/// 显示用户选择
		/// </summary>	
		private void areaBindingSource_CurrentChanged(object sender, EventArgs e)
		{
			if (this.areaBindingSource.Current == null)
				return;

			Area area = (Area)this.areaBindingSource.Current;

			if (area.created == null)
				return;

			SetAreaTextBoxsValues(area);
		}

		/// <summary>
		/// 得到用户选择的Area
		/// </summary>
		/// <returns></returns>
		private Area GetSelectArea()
		{
			if (this.areaBindingSource.Current == null)
				return null;
			Area area = (Area)this.areaBindingSource.Current;
			if (area.created == null)
				return null;
			return area;
		}

		/// <summary>
		/// 检查用户输入的Area信息格式是否正确
		/// </summary>
		/// <returns></returns>
		private bool CheckArea()
		{
			decimal _decimal;

			if (this.TextBox_Area_Centiare.Text.Length < 1)
			{
				this.TextBox_Area_Centiare.Text = "0";
			} else
			{
				if (!decimal.TryParse(this.TextBox_Area_Centiare.Text, out _decimal))
				{
					this.TextBox_Area_Centiare.Focus();
					this.TextBox_Area_Centiare.SelectAll();
					return false;
				}
			}

			if (this.TextBox_Area_Mu.Text.Length < 1)
			{
				this.TextBox_Area_Mu.Text = "0";
			} else
			{
				if (!decimal.TryParse(this.TextBox_Area_Mu.Text, out _decimal))
				{
					this.TextBox_Area_Mu.Focus();
					this.TextBox_Area_Mu.SelectAll();
					return false;
				}
			}
			return true;

		}
		#endregion

		/// <summary>
		/// 刷新Area记录
		/// </summary>
		private void RefreshArea()
		{
			this.Villages.Areas.Load();
			this.areaBindingSource.DataSource = this.Villages.Areas.OrderByDescending(t => t.updated).ToList();
			this.areaBindingSource.MoveFirst();
		}

		/// <summary>
		/// 数据源变更
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void areaBindingSource_DataSourceChanged(object sender, EventArgs e)
		{
			Area _area;
			decimal centiare = 0;
			decimal mu = 0;

			foreach (DataGridViewRow row in this.AreaGridView.Rows)
			{
				if (row.DataBoundItem == null)
					continue;
				_area = (Area)row.DataBoundItem;
				centiare += _area.centiare == null ? 0 : (decimal)_area.centiare;
				mu += _area.mu == null ? 0 : (decimal)_area.mu;
			}
			_area = new Area();
			_area.centiare = centiare;
			_area.mu = mu;
			_area.area_code = "总计：";
			this.areaBindingSource.Add(_area);
		}
		#endregion

		#region Ccompensate相关方法


		#region Ccompensate按钮操作

		/// <summary>
		/// 删除
		/// </summary>		
		private void ButtonDeleteCompensate_Click(object sender, EventArgs e)
		{
			Compensate ccompensate = this.GetSelectCcompensate();
			if (ccompensate == null)
				return;
			this.Session.DataModel.Delete(ccompensate);
			this.RefreshCcompensate();
		}

		/// <summary>
		/// 保存、修改
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ButtonSaveCompensate_Click(object sender, EventArgs e)
		{
			if (this.ButtonCompensateSave.Text == "修改")
			{
				this.Ccompensate = GetSelectCcompensate();
				if (this.Ccompensate == null)
					return;
				//将内容放入文本框
				SetCcompensateTextBoxsValues(this.Ccompensate);
				//取消文本框的只读
				SetCcompensateTextBoxsReadonly(false);
				this.ButtonCompensateCancel.Text = "取消";
				this.ButtonCompensateSave.Text = "保存";

			} else
			{
				try
				{
					if (this.Ccompensate == null)
					{
						this.Ccompensate = new Compensate();
						this.Ccompensate.compensate_id = DataModel.GenerateID(this.Ccompensate);
						this.Ccompensate.Village = this.Villages;
						this.Ccompensate.created = System.DateTime.Now;
					}
					if (GetCcompensateFormInput(ref this.Ccompensate) == null)
					{
						MyMessagebox.Show("输入内容的格式不正确！");
						return;
					}
					this.Session.DataModel.Save(this.Ccompensate);
					SetCcompensateTextBoxsReadonly(true);
					MessageBox.Show("保存成功！");
					this.Session.DataModel.Refresh(System.Data.Objects.RefreshMode.ClientWins, this.Villages.Compensates);
					this.RefreshCcompensate();
					this.ButtonCompensateCancel.Text = "新增";
					this.ButtonCompensateSave.Text = "修改";
				} catch (Exception ex)
				{
					string error = Tools.GetExceptionMessage(ex);
					Log.Error(error);
					MyMessagebox.Error(error);
				}


			}
		}

		/// <summary>
		/// 取消、新增
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ButtonCancelCompensate_Click(object sender, EventArgs e)
		{
			if (this.ButtonCompensateCancel.Text == "新增")
			{
				this.Ccompensate = null;

				//将文本框设置为可编辑
				SetCcompensateTextBoxsReadonly(false);
				//将文本框内容清空
				SetCcompensateTextBoxsValues(null);
				this.ButtonCompensateCancel.Text = "取消";
				this.ButtonCompensateSave.Text = "保存";

			} else
			{
				SetCcompensateTextBoxsReadonly(true);

				Compensate _ccompensate = this.Ccompensate;
				if (_ccompensate == null)
				{
					_ccompensate = GetSelectCcompensate();
				}

				SetCcompensateTextBoxsValues(_ccompensate);
				this.ButtonCompensateCancel.Text = "新增";
				this.ButtonCompensateSave.Text = "修改";
			}
		}

		/// <summary>
		/// 显示用户选择
		/// </summary>	
		private void ccompensateBindingSource_CurrentChanged(object sender, EventArgs e)
		{
			if (this.ccompensateBindingSource.Current == null)
			{
				return;
			}
			Compensate ccompensate = (Compensate)this.ccompensateBindingSource.Current;
			if (ccompensate.created == null)
				return;
			SetCcompensateTextBoxsValues(ccompensate);
		}

		#endregion

		#region Ccompensate其他方法
		/// <summary>
		/// 设置Ccompensate输入框是否只读
		/// </summary>
		/// <param name="readOnly"></param>
		private void SetCcompensateTextBoxsReadonly(bool readOnly)
		{
			this.TextBox_Compensate_Compensate_name.ReadOnly = readOnly;
			this.TextBox_Compensate_Compensate_moner.ReadOnly = readOnly;
			this.TextBox_Compensate_Audit_money.ReadOnly = readOnly;
			this.TextBox_Compensate_Unit.ReadOnly = readOnly;
			this.TextBox_Compensate_Count.ReadOnly = readOnly;
			this.TextBox_Compensate_Price.ReadOnly = readOnly;
			this.TextBox_Compensate_RemarkCompensate.ReadOnly = readOnly;
			this.ccompensateDataGridView.Enabled = readOnly;
		}

		/// <summary>
		/// 将Ccompensate数据放到文本框中（如果area为空则清空文本框）
		/// </summary>
		/// <param name="area">Area</param>
		private void SetCcompensateTextBoxsValues(Compensate ccompensate)
		{
			if (ccompensate == null)
			{
				this.TextBox_Compensate_Compensate_name.Text = "";
				this.TextBox_Compensate_Compensate_moner.Text = "0.00";
				this.TextBox_Compensate_Audit_money.Text = "0.00";
				this.TextBox_Compensate_Unit.Text = "";
				this.TextBox_Compensate_Count.Text = "0";
				this.TextBox_Compensate_Price.Text = "0.00";
				this.TextBox_Compensate_RemarkCompensate.Text = "";
				return;
			}

			this.TextBox_Compensate_Compensate_name.Text = ccompensate.compensate_name;
			this.TextBox_Compensate_Compensate_moner.Text = ccompensate.compensate_moner.ToString();
			this.TextBox_Compensate_Audit_money.Text = ccompensate.audit_money.ToString();
			this.TextBox_Compensate_Unit.Text = ccompensate.unit;
			this.TextBox_Compensate_Count.Text = ccompensate.count.ToString();
			this.TextBox_Compensate_Price.Text = ccompensate.price.ToString();
			this.TextBox_Compensate_RemarkCompensate.Text = ccompensate.remark;
		}

		/// <summary>
		/// 更具用户输入的内容初始化Ccompensate 
		/// </summary>
		/// <param name="area"></param>
		/// <returns></returns>
		private Compensate GetCcompensateFormInput(ref Compensate ccompensate)
		{
			if (!CheckCompensate())
				return null;

			ccompensate.compensate_name = this.TextBox_Compensate_Compensate_name.Text;
			ccompensate.compensate_moner = Convert.ToDecimal(this.TextBox_Compensate_Compensate_moner.Text);
			ccompensate.audit_money = Convert.ToDecimal(this.TextBox_Compensate_Audit_money.Text);
			ccompensate.unit = this.TextBox_Compensate_Unit.Text;
			ccompensate.count = Convert.ToDecimal(this.TextBox_Compensate_Count.Text);
			ccompensate.price = Convert.ToDecimal(this.TextBox_Compensate_Price.Text);
			ccompensate.remark = this.TextBox_Compensate_RemarkCompensate.Text;
			ccompensate.updated = System.DateTime.Now;
			return ccompensate;
		}

		/// <summary>
		/// 检查用户输入格式是否正确
		/// </summary>
		/// <returns></returns>
		private bool CheckCompensate()
		{
			int _int;
			decimal _decimal;

			if (this.TextBox_Compensate_Compensate_moner.Text.Length < 1)
			{
				this.TextBox_Compensate_Compensate_moner.Text = "0";
			} else
			{
				if (!decimal.TryParse(this.TextBox_Compensate_Compensate_moner.Text, out _decimal))
				{
					this.TextBox_Compensate_Compensate_moner.Focus();
					this.TextBox_Compensate_Compensate_moner.SelectAll();
					return false;
				}
			}

			if (this.TextBox_Compensate_Audit_money.Text.Length < 1)
			{
				this.TextBox_Compensate_Audit_money.Text = "0";
			} else
			{
				if (!decimal.TryParse(this.TextBox_Compensate_Audit_money.Text, out _decimal))
				{
					this.TextBox_Compensate_Audit_money.Focus();
					this.TextBox_Compensate_Audit_money.SelectAll();
					return false;
				}
			}

			if (this.TextBox_Compensate_Price.Text.Length < 1)
			{
				this.TextBox_Compensate_Price.Text = "0";
			} else
			{
				if (!decimal.TryParse(this.TextBox_Compensate_Price.Text, out _decimal))
				{
					this.TextBox_Compensate_Price.Focus();
					this.TextBox_Compensate_Price.SelectAll();
					return false;
				}
			}

			if (this.TextBox_Compensate_Count.Text.Length < 1)
			{
				this.TextBox_Compensate_Count.Text = "0";
			} else
			{
				if (!int.TryParse(this.TextBox_Compensate_Count.Text, out _int))
				{
					this.TextBox_Compensate_Count.Focus();
					this.TextBox_Compensate_Count.SelectAll();
					return false;
				}
			}

			return true;

		}

		/// <summary>
		/// 得到用户选择的Area
		/// </summary>
		/// <returns></returns>
		private Compensate GetSelectCcompensate()
		{
			if (this.ccompensateBindingSource.Current == null)
				return null;
			Compensate ccompensate = (Compensate)this.ccompensateBindingSource.Current;
			if (ccompensate.created == null)
				return null;
			return ccompensate;
		}
		#endregion


		/// <summary>
		/// 刷新RefreshCcompensate
		/// </summary>
		private void RefreshCcompensate()
		{
			this.Villages.Compensates.Load();
			this.ccompensateBindingSource.DataSource = this.Villages.Compensates.OrderByDescending(t => t.updated).ToList();
			;
			this.ccompensateBindingSource.MoveFirst();
		}

		private void ccompensateBindingSource_DataSourceChanged(object sender, EventArgs e)
		{
			Compensate ccompensate;
			decimal compensate_moner = 0;
			decimal audit_money = 0;

			foreach (DataGridViewRow row in this.ccompensateDataGridView.Rows)
			{
				if (row.DataBoundItem == null)
					continue;
				ccompensate = (Compensate)row.DataBoundItem;
				compensate_moner += ccompensate.compensate_moner == null ? 0 : (decimal)ccompensate.compensate_moner;
				audit_money += ccompensate.audit_money == null ? 0 : (decimal)ccompensate.audit_money;
			}
			ccompensate = new Compensate();
			ccompensate.compensate_moner = compensate_moner;
			ccompensate.audit_money = audit_money;
			ccompensate.compensate_name = "总计：";
			this.ccompensateBindingSource.Add(ccompensate);
		}

		#endregion


		#region 其他方法

		#endregion

		/// <summary>
		/// 面积打印
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ButtonAreaPrint_Click(object sender, EventArgs e)
		{
			if (this.AreaGridView.Rows.Count == 0)
			{
				MyMessagebox.Show("当前没有记录！");
				return;
			}

			if (this.AreaGridView.Rows.Count > 200)
			{
				DialogResult dialogResult = MyMessagebox.Confirm("当前记录已超过200条，生成Excel的可能较慢，是否继续？");
				if (dialogResult != DialogResult.Yes)
					return;
			}

			List<string> ColumnNames = new List<string>();
			ColumnNames.Add("areacodeDataGridViewTextBoxColumn");
			ColumnNames.Add("areanameDataGridViewTextBoxColumn");
			ColumnNames.Add("centiareDataGridViewTextBoxColumn");
			ColumnNames.Add("groupareaDataGridViewTextBoxColumn");
			ColumnNames.Add("muDataGridViewTextBoxColumn");
			ColumnNames.Add("remarkDataGridViewTextBoxColumn");

			WaitForm waitForm = new WaitForm("请稍候", "正在生成文档，请稍候...");

			ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj) {
				AreaToExcel(this.ccompensateDataGridView, true, ColumnNames);

				waitForm.CloseForm();

			}));

			waitForm.ShowDialog();

		}

		/// <summary>
		/// 赔偿打印
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ButtonCompensatePrint_Click(object sender, EventArgs e)
		{

			if (this.ccompensateDataGridView.Rows.Count == 0)
			{
				MyMessagebox.Show("当前没有记录！");
				return;
			}

			if (this.ccompensateDataGridView.Rows.Count > 200)
			{
				DialogResult dialogResult = MyMessagebox.Confirm("当前记录已超过200条，生成Excel的可能较慢，是否继续？");
				if (dialogResult != DialogResult.Yes)
					return;
			}

			List<string> ColumnNames = new List<string>();
			ColumnNames.Add("compensatenameDataGridViewTextBoxColumn");
			ColumnNames.Add("unitDataGridViewTextBoxColumn");
			ColumnNames.Add("countDataGridViewTextBoxColumn");
			ColumnNames.Add("priceDataGridViewTextBoxColumn");
			ColumnNames.Add("compensatemonerDataGridViewTextBoxColumn");
			ColumnNames.Add("auditmoneyDataGridViewTextBoxColumn");
			ColumnNames.Add("remarkDataGridViewTextBoxColumn1");

			WaitForm waitForm = new WaitForm("请稍候", "正在生成文档，请稍候...");

			ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj) {
				CompensateToExcel(this.ccompensateDataGridView, true, ColumnNames);

				waitForm.CloseForm();

			}));

			waitForm.ShowDialog();

		}


		public void CompensateToExcel(DataGridView gridView, bool isShowExcle, List<string> ColumnNames)
		{
			/*
			if (gridView.Rows.Count == 0)
				return;

			Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
			excel.Application.Workbooks.Add(true);


			Microsoft.Office.Interop.Excel._Worksheet sheet = (Microsoft.Office.Interop.Excel._Worksheet)excel.Sheets.get_Item(1);
			sheet.Name = "当涂县国家建设征拨地图地附属物补偿表";

			//excel.Visible = true;
			#region 多行表头
			Range range1 = (Range)sheet.get_Range("a1", "g1");
			range1.Merge(false);
			range1.Value2 = "当涂县国家建设征拨地图地附属物补偿表";

			sheet.Cells[2, 1] = "";//2010年4月9日

			sheet.Cells[2, 7] = "";//第48号

			sheet.Cells[3, 1] = "征用单位";


			Range range5 = (Range)sheet.get_Range("b3", "d3");
			range5.Merge(false);
			range5.Value2 = "被征用单位";


			Range range6 = (Range)sheet.get_Range("e3", "f3");
			range6.Merge(false);
			range6.Value2 = "建设项目";


			sheet.Cells[3, 7] = "工程名称";



			sheet.Cells[4, 1] = "当涂经济开发区";


			Range range7 = (Range)sheet.get_Range("b4", "d4");
			range7.Merge(false);
			range7.Value2 = "竹山村";


			Range range8 = (Range)sheet.get_Range("e4", "f4");
			range8.Merge(false);
			range8.Value2 = "竹山安置小区二期";


			sheet.Cells[4, 7] = "基础设施";




			#endregion

			//生成字段名称
			for (int i = 0; i < ColumnNames.Count; i++)
			{
				string HeaderText = gridView.Columns[ColumnNames[i]].HeaderText;
				sheet.Cells[5, i + 1] = HeaderText;
			}


			//填充数据	
			for (int i = 0; i < gridView.RowCount; i++)
			{
				int rowIndex = i + 6;
				for (int j = 0; j < ColumnNames.Count; j++)
				{
					DataGridViewCell cell = gridView[ColumnNames[j], i];

					if (cell.Value == null)
						continue;

					if (cell.ValueType == typeof(string))
					{
						sheet.Cells[rowIndex, j + 1] = "'" + cell.Value.ToString();
					}
					else if (cell.ValueType == typeof(DateTime) || cell.ValueType == typeof(System.Nullable<DateTime>))
					{
						sheet.Cells[rowIndex, j + 1] = ((DateTime)cell.Value).ToString("yy年MM月dd日");

					}
					else
					{
						sheet.Cells[rowIndex, j + 1] = cell.FormattedValue.ToString();
					}
				}

			}
			excel.Visible = true;
			///前5行距中
			((Range)sheet.get_Range("a1", "g5")).HorizontalAlignment = XlHAlign.xlHAlignCenter;

			//表头粗体
			((Range)sheet.get_Range("a1", "g1")).Font.Bold = true;

			((Range)sheet.get_Range("a3", "g3")).Font.Bold = true;

			((Range)sheet.get_Range("a5", "g5")).Font.Bold = true;

			//加边框
			int rowCount = this.ccompensateDataGridView.Rows.Count + 5;

			sheet.get_Range("a3", "g" + rowCount.ToString()).Borders.Weight = XlBorderWeight.xlMedium;


			//设置自动调整列宽
			for (int i = 0; i < ColumnNames.Count; i++)
			{
				((Range)sheet.Cells[Type.Missing, i + 1]).EntireColumn.AutoFit();
			}
			*/

			return;
		}

		public void AreaToExcel(DataGridView gridView, bool isShowExcle, List<string> ColumnNames)
		{
			/*
			if (gridView.Rows.Count == 0)
				return;

			Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
			excel.Application.Workbooks.Add(true);


			Microsoft.Office.Interop.Excel._Worksheet sheet = (Microsoft.Office.Interop.Excel._Worksheet)excel.Sheets.get_Item(1);
			sheet.Name = "当涂县国家建设征拨地图地附属物补偿表";

			#region 多行表头
			Range range1 = (Range)sheet.get_Range("a1", "g1");
			range1.Merge(false);
			range1.Value2 = "当涂县国家建设征拨地图地附属物补偿表";

			sheet.Cells[2, 1] = "";//2010年4月9日

			sheet.Cells[2, 7] = "";//第48号

			sheet.Cells[3, 1] = "征用单位";


			Range range5 = (Range)sheet.get_Range("b3", "d3");
			range5.Merge(false);
			range5.Value2 = "被征用单位";


			Range range6 = (Range)sheet.get_Range("e3", "f3");
			range6.Merge(false);
			range6.Value2 = "建设项目";


			sheet.Cells[3, 7] = "工程名称";



			sheet.Cells[4, 1] = "当涂经济开发区";


			Range range7 = (Range)sheet.get_Range("b4", "d4");
			range7.Merge(false);
			range7.Value2 = "竹山村";


			Range range8 = (Range)sheet.get_Range("e4", "f4");
			range8.Merge(false);
			range8.Value2 = "竹山安置小区二期";


			sheet.Cells[4, 7] = "基础设施";




			#endregion

			//生成字段名称
			for (int i = 0; i < ColumnNames.Count; i++)
			{
				string HeaderText = gridView.Columns[ColumnNames[i]].HeaderText;
				sheet.Cells[5, i + 1] = HeaderText;
			}


			//填充数据	
			for (int i = 0; i < gridView.RowCount; i++)
			{
				int rowIndex = i + 6;
				for (int j = 0; j < ColumnNames.Count; j++)
				{
					DataGridViewCell cell = gridView[ColumnNames[j], i];

					if (cell.Value == null)
						continue;

					if (cell.ValueType == typeof(string))
					{
						sheet.Cells[rowIndex, j + 1] = "'" + cell.Value.ToString();
					}
					else if (cell.ValueType == typeof(DateTime) || cell.ValueType == typeof(System.Nullable<DateTime>))
					{
						sheet.Cells[rowIndex, j + 1] = ((DateTime)cell.Value).ToString("yy年MM月dd日");
					}
					else
					{
						sheet.Cells[rowIndex, j + 1] = cell.FormattedValue.ToString();
					}
				}

			}
			excel.Visible = true;
			///前5行距中
			((Range)sheet.get_Range("a1", "g5")).HorizontalAlignment = XlHAlign.xlHAlignCenter;

			//表头粗体
			((Range)sheet.get_Range("a1", "g1")).Font.Bold = true;

			((Range)sheet.get_Range("a3", "g3")).Font.Bold = true;

			((Range)sheet.get_Range("a5", "g5")).Font.Bold = true;

			//加边框
			int rowCount = this.ccompensateDataGridView.Rows.Count + 5;

			sheet.get_Range("a3", "g" + rowCount.ToString()).Borders.Weight = XlBorderWeight.xlMedium;


			//设置自动调整列宽
			for (int i = 0; i < ColumnNames.Count; i++)
			{
				((Range)sheet.Cells[Type.Missing, i + 1]).EntireColumn.AutoFit();
			}
			 * */

		}

		/// <summary>
		/// 验证用户权限
		/// </summary>
		private void CheckUser(Session session)
		{
			//权限验证
			if (session == null || Session.User == null || Session.User.role.Equals("general"))
			{
				this.LinkLabelFile.Enabled = false;
				this.ButtonAreaCancel.Enabled = false;
				this.ButtonAreaDelete.Enabled = false;
				this.ButtonAreaSave.Enabled = false;
				//this.ButtonAreaPrint.Enabled = false;
				this.ButtonCompensateCancel.Enabled = false;
				this.ButtonCompensateDelete.Enabled = false;
				//this.ButtonCompensatePrint.Enabled = false;
				this.ButtonCompensateSave.Enabled = false;
				this.ButtonDeleteFile.Enabled = false;

			}
		}




	}
}


